from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
from bs4 import BeautifulSoup
import time
import re
import json
#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    soup = BeautifulSoup(response.text,"html5lib")

    Pages = soup.select(".path")[0].text.strip()
    px = re.compile('([0-9]+)')
    allNum = px.findall(Pages)[0]

    lis = soup.select(".boxListLi5")

    allData = []

    for li in lis:
        tempData = resultData()
        try:
            tempData["titleName"] = li.select("h2 > a")[0].text.strip()
        except:
            continue
        try:
            tempData["titleHref"] = "http://ruc.chineseall.cn" + li.select("h2 > a")[0]["href"]
        except:
            continue
        try:
            tempData["author"] = [li.select('.other span')[0].text.strip().split("/")[0]]
        except:
            continue
        try:
            tempData["source"] = li.select('.other span')[0].text.strip().split("/")[1]
        except:
            continue
        try:
            tempData["publishTime"] = li.select('.other span')[0].text.strip().split("/")[2]
        except:
            continue
        allData.append(tempData)

    return allData,allNum

def index(request):

    headers = {
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    page = 1
    data = json.loads(request.POST.get('data', '1'))

    url = "http://ruc.chineseall.cn/org/show/1530/search/{}/{}".format(data["values"][0],page - 1)
    response = requests.get(url, headers=headers)
    allData, allNum = formData(response)

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")